﻿namespace Nello.Data
{
    using System;
    using System.Collections;
    using System.Data;

    public class Business
    {
        public static int Add(Hashtable ht, string tableName)
        {
            return Add(ht, tableName, null);
        }

        public static int Add(Hashtable ht, string tableName, ILinker linker)
        {
            string sql = "INSERT INTO {2} ({0}) VALUES ({1}) ";
            sql = string.Format(sql, SqlUtil.JoinKeys(ht), SqlUtil.JoinValues(ht), tableName);
            if (linker == null)
            {
                return LinkerUtil.ExecuteNonQuery(sql);
            }
            return linker.ExecuteNonQuery(sql);
        }

        public static int Delete(string tableName, string key, string value)
        {
            return Delete(tableName, key, value, null);
        }

        public static int Delete(string tableName, string key, string value, ILinker linker)
        {
            string sql = "delete {0} where {1}='{2}'";
            sql = string.Format(sql, tableName, key, value);
            if (linker == null)
            {
                return LinkerUtil.ExecuteNonQuery(sql);
            }
            return linker.ExecuteNonQuery(sql);
        }

        public static DataTable GetEmptyTable(string tableName)
        {
            return GetEmptyTable(tableName, null);
        }

        public static DataTable GetEmptyTable(string tableName, ILinker linker)
        {
            string sql = "select * from {0} where 1=0";
            sql = string.Format(sql, tableName);
            if (linker != null)
            {
                return linker.ExecuteDataTable(sql);
            }
            return LinkerUtil.ExecuteDataTable(sql);
        }

        public static DataTable GetItem(string tableName, string key, string value)
        {
            return GetItem(tableName, key, value, null);
        }

        public static DataTable GetItem(string tableName, string key, string value, ILinker linker)
        {
            string sql = "select * from {0} where {1}='{2}'";
            sql = string.Format(sql, tableName, key, value);
            if (linker != null)
            {
                return linker.ExecuteDataTable(sql);
            }
            return LinkerUtil.ExecuteDataTable(sql);
        }

        public static DataTable GetItems(string tableName)
        {
            return GetItems(tableName, "");
        }

        public static DataTable GetItems(string tableName, string orderby)
        {
            return GetItems(tableName, orderby, null);
        }

        public static DataTable GetItems(string tableName, string orderby, ILinker linker)
        {
            string sql = "select * from {0} ";
            if (orderby != "")
            {
                sql = sql + "order by " + orderby;
            }
            sql = string.Format(sql, tableName);
            DataTable dt = new DataTable();
            if (linker != null)
            {
                return linker.ExecuteDataTable(sql);
            }
            return LinkerUtil.ExecuteDataTable(sql);
        }

        public static int Modify(Hashtable ht, string tableName, string keyName)
        {
            return Modify(ht, tableName, keyName, null);
        }

        public static int Modify(Hashtable ht, string tableName, string keyName, ILinker linker)
        {
            string[] keys = keyName.Split(",".ToCharArray());
            string sql = "";
            string[] ids = new string[keys.Length];
            sql = "update {0} set {1} where 1=1 ";
            for (int i = 0; i < ids.Length; i++)
            {
                ids[i] = SqlUtil.PopItem(ht, keys[i]);
                string CS$0$0001 = sql;
                sql = CS$0$0001 + " and " + keys[i] + "='" + ids[i] + "'";
            }
            sql = string.Format(sql, tableName, SqlUtil.JoinPairs(ht));
            if (linker == null)
            {
                return LinkerUtil.ExecuteNonQuery(sql);
            }
            return linker.ExecuteNonQuery(sql);
        }
    }
}

